package com.icore.api.impl;

import java.util.HashMap;
import java.util.Map;

import com.icore.AppContext;
import com.icore.AppException;
import com.icore.api.ICoreGet2Api;
import com.icore.bean.GoucheBaseInfo;
import com.icore.bean.GoucheList;
import com.icore.bean.RetRes;
import com.icore.bean.User;
import com.icore.common.ConfigManager;
import com.icore.http.ApiClient;

/**
 * 核心接口实现类
 * 
 */
public class ICoreGet2ApiImpl implements ICoreGet2Api {

	@Override
	public User login(String username, String pwd) throws AppException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		params.put("password", pwd);

		String loginurl = ConfigManager.SERVER_URI + ConfigManager.SERVER_URI_LOGIN;
		try {
			return User.parse(ApiClient.httpPost(AppContext.getInstance(), loginurl, params, null));
		} catch (Exception e) {
			if (e instanceof AppException)
				throw (AppException) e;
			throw AppException.network(e);
		}
	}

	@Override
	public GoucheList getGoucheList(String userID) throws AppException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("user_id", userID);

		String gouchelisturl = ConfigManager.SERVER_URI + ConfigManager.SERVER_URI_GOUCHE_LIST;
		try {
			return GoucheList.parse(ApiClient.httpPost(AppContext.getInstance(), gouchelisturl, params, null));
		} catch (Exception e) {
			if (e instanceof AppException)
				throw (AppException) e;
			throw AppException.network(e);
		}
	}

	@Override
	public RetRes updateState(String carNo) throws AppException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("car_no", carNo);

		String updatestateurl = ConfigManager.SERVER_URI + ConfigManager.SERVER_URI_UPDATE_STATE;
		try {
			return RetRes.parse(ApiClient.httpPost(AppContext.getInstance(), updatestateurl, params, null));
		} catch (Exception e) {
			if (e instanceof AppException)
				throw (AppException) e;
			throw AppException.network(e);
		}
	}

	@Override
	public RetRes insertBI(Map<String, Object> params) throws AppException {
		String gouche_insert_baseinfo_url = ConfigManager.SERVER_URI + ConfigManager.SERVER_URI_GOUCHE_INSERT_BASEINFO;
		try {
			return RetRes.parse(ApiClient.httpPost(AppContext.getInstance(), gouche_insert_baseinfo_url, params, null));
		} catch (Exception e) {
			if (e instanceof AppException)
				throw (AppException) e;
			throw AppException.network(e);
		}
	}

	@Override
	public GoucheBaseInfo queryBI(Map<String, Object> params) throws AppException {
		String gouche_query_baseinfo_url = ConfigManager.SERVER_URI + ConfigManager.SERVER_URI_GOUCHE_QUERY_BASEINFO;
		try {
			return GoucheBaseInfo.parse(ApiClient.httpPost(AppContext.getInstance(), gouche_query_baseinfo_url, params, null));
		} catch (Exception e) {
			if (e instanceof AppException)
				throw (AppException) e;
			throw AppException.network(e);
		}
	}

}
